/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.ui_builders;

import br.ufes.cwtools.joinus.profile_manager.profile.Personal;
import br.ufes.cwtools.joinus.profile_manager.profile.Profile;
import br.ufes.cwtools.joinus.R;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;


/*
 * this class does all the programatic UI work
 * it is also responsable for exporting a object containing the data
 * acquired from the UI  
*/
public class PersonalUI extends ProfileUI {
	View myView;
	boolean changed=false;
	boolean bodyArt[], turnOns[];
	Spinner eyes, hair, build, looks, bestFeat;
	
	public PersonalUI() {
		this.layoutID = R.layout.personal;
	}

	public String toString() {
		return "Personal";
	}

	private void SetUpSpinners(View v) {
		Spinner s = eyes = (Spinner) v.findViewById(R.id.eye_color_chooser);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(v
				.getContext(), R.array.eye_color,
				android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		hair = s = (Spinner) v.findViewById(R.id.hair_color_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.hair_color, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		build = s = (Spinner) v.findViewById(R.id.build_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.build, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);
		
		looks = s = (Spinner) v.findViewById(R.id.looks_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.looks, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);
		
		bestFeat = s = (Spinner) v.findViewById(R.id.best_feat_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.feature, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

	}
	
	public void setUpWidgets(View v) {
		myView = v;
		changed = true; 
		SetUpSpinners(v);
		SetupButtons();
	}

	
	private void SetupButtons() {
		Button body = (Button) myView.findViewById(R.id.body_art_chooser);
		body.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final Dialog bodyArtDialog = new Dialog(v.getContext());
				bodyArtDialog.setContentView(R.layout.body_art_dialog);
				Button todo = (Button) bodyArtDialog
						.findViewById(R.id.body_art_quit);
				bodyArtDialog.setTitle("Body Art");

				if(PersonalUI.this.bodyArt!=null){
					((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb0)).setChecked(PersonalUI.this.bodyArt[0]);
					((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb1)).setChecked(PersonalUI.this.bodyArt[1]);
					((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb2)).setChecked(PersonalUI.this.bodyArt[2]);
					((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb3)).setChecked(PersonalUI.this.bodyArt[3]);
					((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb4)).setChecked(PersonalUI.this.bodyArt[4]);
					((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb5)).setChecked(PersonalUI.this.bodyArt[5]);
				}
				todo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						if(PersonalUI.this.bodyArt==null)PersonalUI.this.bodyArt = new boolean[6];
						PersonalUI.this.bodyArt[0]=((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb0)).isChecked();
						PersonalUI.this.bodyArt[1]=((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb1)).isChecked();
						PersonalUI.this.bodyArt[2]=((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb2)).isChecked();
						PersonalUI.this.bodyArt[3]=((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb3)).isChecked();
						PersonalUI.this.bodyArt[4]=((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb4)).isChecked();
						PersonalUI.this.bodyArt[5]=((CheckBox)bodyArtDialog.findViewById(R.id.body_art_cb5)).isChecked();
						
						bodyArtDialog.dismiss();
					}
				});
				bodyArtDialog.show();
			}
		});
		
		Button turnOns = (Button) myView.findViewById(R.id.turn_ons_chooser);
		turnOns.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final Dialog turonsArtDialog = new Dialog(v.getContext());
				turonsArtDialog.setContentView(R.layout.turn_ons_dialog);
				Button todo = (Button) turonsArtDialog
						.findViewById(R.id.turn_ons_quit);
				turonsArtDialog.setTitle("Turn Ons");
				if(PersonalUI.this.turnOns!=null){
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb0)).setChecked(PersonalUI.this.turnOns[0]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb1)).setChecked(PersonalUI.this.turnOns[1]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb2)).setChecked(PersonalUI.this.turnOns[2]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb3)).setChecked(PersonalUI.this.turnOns[3]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb4)).setChecked(PersonalUI.this.turnOns[4]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb5)).setChecked(PersonalUI.this.turnOns[5]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb6)).setChecked(PersonalUI.this.turnOns[6]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb7)).setChecked(PersonalUI.this.turnOns[7]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb8)).setChecked(PersonalUI.this.turnOns[8]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb9)).setChecked(PersonalUI.this.turnOns[9]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb10)).setChecked(PersonalUI.this.turnOns[10]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb11)).setChecked(PersonalUI.this.turnOns[11]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb12)).setChecked(PersonalUI.this.turnOns[12]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb13)).setChecked(PersonalUI.this.turnOns[13]);
					((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb14)).setChecked(PersonalUI.this.turnOns[14]);
					
				}
				
				todo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						
						if(PersonalUI.this.turnOns==null)PersonalUI.this.turnOns = new boolean[15];
						PersonalUI.this.turnOns[0]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb0)).isChecked();
						PersonalUI.this.turnOns[1]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb1)).isChecked();
						PersonalUI.this.turnOns[2]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb2)).isChecked();
						PersonalUI.this.turnOns[3]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb3)).isChecked();
						PersonalUI.this.turnOns[4]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb4)).isChecked();
						PersonalUI.this.turnOns[5]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb5)).isChecked();
						PersonalUI.this.turnOns[6]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb6)).isChecked();
						PersonalUI.this.turnOns[7]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb7)).isChecked();
						PersonalUI.this.turnOns[8]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb8)).isChecked();
						PersonalUI.this.turnOns[9]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb9)).isChecked();
						PersonalUI.this.turnOns[10]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb10)).isChecked();
						PersonalUI.this.turnOns[11]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb11)).isChecked();
						PersonalUI.this.turnOns[12]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb12)).isChecked();
						PersonalUI.this.turnOns[13]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb13)).isChecked();
						PersonalUI.this.turnOns[14]=((CheckBox)turonsArtDialog.findViewById(R.id.turn_ons_cb14)).isChecked();
						
						
						turonsArtDialog.dismiss();
					}
				});
				turonsArtDialog.show();
			}
		});
	}
	
	@Override
	public void importPro(Profile p) {
		Personal pe = (Personal) p;
		((EditText) myView.findViewById(R.id.headline_uni_text)).setText(pe.headline);
		((EditText) myView.findViewById(R.id.about_me_text)).setText(pe.firstThing);
		((EditText) myView.findViewById(R.id.height_text)).setText(pe.heigth+"");
		eyes.setSelection(pe.eyeColor);
		hair.setSelection(pe.hairColor);
		build.setSelection(pe.build);
		looks.setSelection(pe.looks);
		bestFeat.setSelection(pe.bestFeat);
		((EditText) myView.findViewById(R.id.turn_offs_text)).setText(pe.turnOffs);
		((EditText) myView.findViewById(R.id.first_text)).setText(pe.firstDate);
		((EditText) myView.findViewById(R.id.past_text)).setText(pe.pastRelation);
		((EditText) myView.findViewById(R.id.five_text)).setText(pe.fiveThi);
		((EditText) myView.findViewById(R.id.bath_text)).setText(pe.bedroom);
		((EditText) myView.findViewById(R.id.ideal_text)).setText(pe.idealMatch);

	}
	
	
	@Override
	public Profile export() {
		if(changed){
			Personal personal = new Personal();
			
			personal.headline =  ((EditText) myView.findViewById(R.id.headline_uni_text))
			.getText().toString();

			personal.fiveThi = ((EditText) myView.findViewById(R.id.five_text))
			.getText().toString();
			
			personal.firstThing = ((EditText) myView.findViewById(R.id.about_me_text))
			.getText().toString();
			
			try {
				personal.heigth = Integer.parseInt(((EditText) myView.findViewById(R.id.height_text))
				.getText().toString());
			} catch (NumberFormatException e) {
				personal.heigth=0;
			}
			
			personal.eyeColor = eyes.getSelectedItemPosition();
			
			personal.hairColor = hair.getSelectedItemPosition();
			
			personal.build = build.getSelectedItemPosition();
			
			personal.looks = looks.getSelectedItemPosition();
			
			personal.bestFeat = bestFeat.getSelectedItemPosition();
			
			personal.turnOffs = ((EditText) myView.findViewById(R.id.turn_offs_text))
			.getText().toString();
			
			personal.firstDate = ((EditText) myView.findViewById(R.id.first_text))
			.getText().toString();
			
			personal.pastRelation = ((EditText) myView.findViewById(R.id.past_text))
			.getText().toString();
			
		
			personal.bedroom = ((EditText) myView.findViewById(R.id.bath_text))
			.getText().toString();
			
			personal.idealMatch = ((EditText) myView.findViewById(R.id.ideal_text))
			.getText().toString();
			
			return personal;
		}
		return null;
	}
}
